package org.tity.test.java.lang;

import java.util.*;  
import java.io.*;  
public class CountChars{  
    public static void main(String args[]) throws UnsupportedEncodingException{  
          
        String ss=new String("javaeye我是cjjwzs阿".getBytes(),"GBK");
        CountChars c=new CountChars();  
        c.count(ss);  
    }  
    private void count(String s){  
        Map<String,Integer> counter=new HashMap<String,Integer>();  
        List<String> lSingel=new LinkedList<String>();  
        List<String> lDouble=new LinkedList<String>();  
        int singelC=0,doubleC=0;  
            for(int i=0;i<s.length();i++){  
                int c=String.valueOf(s.charAt(i)).getBytes().length;  
                System.out.println(s.charAt(i)+" = "+c);
                if(counter.containsKey(String.valueOf(s.charAt(i)))==false){  
                    counter.put(String.valueOf(s.charAt(i)),1);  
                }else{  
                    int tempc=counter.get(String.valueOf(s.charAt(i)));  
                    tempc++;  
                    counter.put(String.valueOf(s.charAt(i)),tempc);  
                }  
                switch(c){  
                    case 1:   
                        singelC++;  
                        if(lSingel.contains(String.valueOf(s.charAt(i)))){  
                            break;  
                        }else{  
                            lSingel.add(String.valueOf(s.charAt(i)));  
                        }  
                        break;  
                    case 2:   
                        doubleC++;  
                        if(lDouble.contains(String.valueOf(s.charAt(i)))){  
                            break;  
                        }else{  
                            lDouble.add(String.valueOf(s.charAt(i)));  
                        }  
                        break;  
                }  
            }  
            Set<String> set=counter.keySet();  
            Iterator it=set.iterator();  
            System.out.println("各个字符的个数为:");  
            while(it.hasNext()){  
                String sss=String.valueOf(it.next());  
                System.out.print(sss+"="+counter.get(sss)+",");  
            }  
            System.out.println();  
            System.out.println("单字节字符数目："+singelC+",双字节字符数目："+doubleC);  
            System.out.println("单字节字符为"+lSingel);  
            System.out.println("双字节字符为"+lDouble);  
        }  
}  